var Diary = {
    initialize : function(settings) {
        layui.use(['form','element','layedit','table'], function(){
            var form = layui.form,
                element = layui.element,
                layedit = layui.layedit,
                table = layui.table;

            table.render({
                elem: '#diary'
                ,even: true
                ,url:Base.url('page')
                ,cols: [[
                    {type:'checkbox', fixed: 'left'}
                    ,{field:'id', title: 'ID', sort: true}
                    ,{field:'addtime',title: '创建时间'}
                    ,{field:'content', title: '内容'}
                    ,{title: '操作',align:'center', toolbar: '#barDemo'}
                ]]
                ,page: true
            });

            //编辑和删除
            table.on('tool(diary)', function(obj){
                var data = obj.data;
                if(obj.event === 'del'){
                    layer.confirm('你确定要删除分类吗？', function(index){
                        Diary.delete_diy(data.id);
                        layer.close(index);
                    });
                } else if(obj.event === 'edit'){
                    Diary.edit_diy(data.id);
                }
            });

            //操作
            var active = {
                add_diary: function(){ //添加分类
                    add_diary();
                }
                ,delete_diary: function(){ //批量删除分类
                    var checkStatus = table.checkStatus('diary'),
                        data = checkStatus.data;
                    Diary.delete_diys(data);
                }
            };

            $('.operateTable .layui-btn').on('click', function(){
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });

            //添加日记    
            var add_diary = function(){
                var editIndexs = '';
                layer.open({
                    type : 1,
                    offset:'150px',
                    area : '700px',
                    title : '添加日记',
                    btn:false,
                    content : $('#add_diary_container').html(),
                    success : function(layero, index){
                        editIndexs = layedit.build('editorDiary',{height: 150,tool: ['face','left', 'center', 'right', '|','link','unlink']});
                        form.render();
                        form.verify({
                            content: function(value){
                                value = $.trim(layedit.getText(editIndexs));
                                if(value == ''){
                                    return '请输入日记内容';
                                }
                                layedit.sync(editIndexs);//同步内容
                            }
                        })

                        form.on('submit(add-diary)', function(data){
                            layer.confirm('你确定要添加日记吗？',{
                                icon:3,
                                offset:'120px',
                                btn:['确定','取消'],
                                yes: function(index){
                                    var content = data.field['content'];

                                    Base.ajax({
                                        url :Base.url('addDiary'),
                                        type:'post',
                                        data : {content: content},
                                        success : function(data) {
                                            if (data.status == 'ok') {
                                                layer.msg(data.results,{time:1000},function(){
                                                    layer.closeAll();
                                                    Base.redirect(Base.url('diary'));
                                                });
                                            } else {
                                                layer.msg(data.errdesc,{time:1500});
                                                return false;
                                            }
                                        }
                                    });
                                }
                            });              
                        });
                    }
                });     
            }
            form.render();
            
        });
    },

    //删除日记
    delete_diy : function(id){
        layer.load(3);
        Base.ajax({
            url :Base.url('deleteDiary'),
            type:'post',
            data : {id:id},
            success : function(data) {
                if (data.status == 'ok') {
                    layer.closeAll('loading');
                    layer.msg(data.results,{time:1000},function(){
                        layer.closeAll();
                        Base.redirect(Base.url('diary'));
                    });
                } else {
                    layer.msg(data.errdesc,{time:1500});
                    layer.closeAll('loading');
                    return false;
                }
            }
        });
    },
        
    //批量删除
    delete_diys : function(data){
        var ids = [];
        $.each(data, function(index,value){
            ids.push(value.id);
        });

        if(ids.length == 0){
            layer.msg('请选择要删除的日记',{time:2000});
            return false;
        }

        layer.confirm('你确定要删除这些日记吗？',{
            icon:3,
            btn:['确定','取消'],
            yes: function(index){
                layer.load(3);
                Base.ajax({
                    url :Base.url('deleteDiary'),
                    type:'post',
                    data : {id:ids},
                    success : function(data) {
                        if (data.status == 'ok') {
                            layer.closeAll('loading');
                            layer.msg(data.results,{time:1000},function(){
                                layer.closeAll();
                                Base.redirect(Base.url('diary'));
                            });
                        } else {
                            layer.msg(data.errdesc,{time:1500});
                            layer.closeAll('loading');
                            return false;
                        }
                    }
                });
            }
        });
    },

    //编辑日记
    edit_diy : function(id){
        var edIndex = '';
        layer.load(3);
        Base.ajax({
            url :Base.url('findOne'),
            type:'post',
            data : {id:id},
            success : function(data) {
                layer.closeAll('loading');
                if (data.status == 'ok') {
                    layer.open({
                        type : 1,
                        offset:'150px',
                        area : '1000px',
                        title : '编辑日志',
                        btn:false,
                        closeBtn:2,
                        content : $('#edit_diary_container').html(),
                        success : function(layero, index){
                            layui.use(['form','element','layedit'], function(){
                                var form = layui.form,
                                    element = layui.element,
                                    layedit = layui.layedit;

                                layero.find('textarea[name="content"]').val(data.results.content);

                                form.render();
                                
                                edIndex = layedit.build('edit_editorDiary',{height: 250,tool: ['left', 'center', 'right', '|', 'face','code','link','unlink']});
                                form.verify({
                                    content: function(value){
                                        value = $.trim(layedit.getText(edIndex));
                                        if(value == ''){
                                            return '请输入文章内容';
                                        }
                                        layedit.sync(edIndex);//同步内容
                                    }
                                });


                                form.on('submit(edit-diary)', function(data){
                                    layer.confirm('你确定要更新日志吗？',{
                                        icon:3,
                                        offset:'200px',
                                        btn:['确定','取消'],
                                        closeBtn:2,
                                        yes: function(index){
                                            var content = data.field['content'],
                                                datas = {id:id,content:content};

                                            Base.ajax({
                                                url :Base.url('editDiary'),
                                                type:'post',
                                                data : {data:datas},
                                                success : function(data) {
                                                    if (data.status == 'ok') {
                                                        layer.msg(data.results,{time:1000},function(){
                                                            layer.closeAll();
                                                            Base.redirect(Base.url('article'));
                                                        });
                                                    } else {
                                                        layer.msg(data.errdesc,{time:1500});
                                                        layer.closeAll('loading');
                                                        return false;
                                                    }
                                                }
                                            });
                                        }
                                    });
                                });
                            });
                        }
                    });
                } else {
                    layer.msg(data.errdesc,{offset:'120px',time:1500});
                    layer.closeAll('loading');
                    return false;
                }
            }
        });
    }
}